#include "stdafx.h"
#include "mirror.h"

mirror::mirror(void)
{
	mWindow = NULL; 
	mCamera = NULL;   
	mPlane = NULL; 
	mPlaneEnt = NULL; 
	mReflectCam = NULL; 
	mPlaneNode = NULL; 
	mSceneMgr = NULL; 
}

mirror::~mirror(void)
{
	rttTex->removeListener(this);
	Root::getSingleton().getRenderSystem()->detachRenderTarget(rttTex->getName());
  	//TexturePtr texture;
    TextureManager::getSingleton().remove((Ogre::ResourcePtr)texture);
}

void mirror::updata()
{
	mReflectCam->setOrientation(mCamera->getOrientation());
	mReflectCam->setPosition(mCamera->getPosition());

}

void mirror::init(Ogre::RenderWindow* Window, 
				  Ogre::Camera* Camera,
				  MovablePlane*  Plane,
				  Entity*        PlaneEnt,
				  SceneNode*     PlaneNode,
				  Ogre::SceneManager* SceneMgr)
{
	mWindow = Window;
	mCamera = Camera;
	mPlane = Plane;
	mPlaneEnt = PlaneEnt;
	mPlaneNode = PlaneNode;
	mSceneMgr = SceneMgr;

	mPlaneNode->attachObject(mPlane);
	  texture = TextureManager::getSingleton().createManual( "RttTex", 
		ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, 
		512, 512, 0, PF_A8R8G8B8, TU_RENDERTARGET );
	rttTex = texture->getBuffer()->getRenderTarget();
	{
		mReflectCam = mSceneMgr->createCamera("ReflectCam");
		mReflectCam->setNearClipDistance(mCamera->getNearClipDistance());
		mReflectCam->setFarClipDistance(mCamera->getFarClipDistance());
		mReflectCam->setAspectRatio(
			(Real)mWindow->getViewport(0)->getActualWidth() / 
			(Real)mWindow->getViewport(0)->getActualHeight());
		mReflectCam->setFOVy (mCamera->getFOVy());


		Viewport *v = rttTex->addViewport( mReflectCam );
		v->setClearEveryFrame( true );
		v->setBackgroundColour( ColourValue::Black );

		//MaterialPtr mat = MaterialManager::getSingleton().create("RttMat",
			//ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

		MaterialPtr mat = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName("ref"));
		//mat->getTechnique(0)->getPass(0)->setAlphaRejectValue()
		TextureUnitState* t ;//= mat->getTechnique(0)->getPass(0)->createTextureUnitState("RustedMetal.jpg");
		t = mat->getTechnique(0)->getPass(0)->getTextureUnitState("rtt");
		// Blend with base texture
 		t->setColourOperationEx(LBX_BLEND_MANUAL, LBS_TEXTURE, LBS_CURRENT, ColourValue::White, 
 			ColourValue::White, 0.95);
		t->setTextureName("RttTex");
		t->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);
		t->setProjectiveTexturing(true, mReflectCam);
		rttTex->addListener(this);



		mat = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName("SD_002_zhandi"));

		if (!mat.isNull())
		{
			//t = mat->getTechnique(0)->getPass(0)->getTextureUnitState("reffr");
			//t->setTextureName("RttTex");
			//t->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);
			//t->setProjectiveTexturing(true, mReflectCam);
		}

		// set up linked reflection
		mReflectCam->enableReflection(mPlane);
		// Also clip
		mReflectCam->enableCustomNearClipPlane(mPlane);
	}

	mPlaneEnt->setMaterialName("ref");
}

  